/**
 * Apr 27, 2010
 */
package org.tomale.id.inventory.management.ui.wizards;

import java.util.List;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.tomale.id.ProviderException;
import org.tomale.id.inventory.Unit;
import org.tomale.id.inventory.management.Activator;
import org.tomale.id.inventory.management.IInventoryProvider;
import org.tomale.id.inventory.management.InventoryManager;

/**
 * @author ferd
 *
 */
public class ItemGroupOverviewPage extends WizardPage {

	public static final String PAGE_NAME = "Item Group";
	public static final String PAGE_TITLE = "New item group";
	public static final String PAGE_DESC = "Create new item group";
	
	Text _name;
	Text _desc;
	Combo _unit;
	
	IInventoryProvider _provider;
	
	ModifyListener _modify = new ModifyListener() {
		
		@Override
		public void modifyText(ModifyEvent e) {
			updatePageComplete();
		}
	};
	
	public ItemGroupOverviewPage(){
		super(PAGE_NAME,
				PAGE_TITLE,
				Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/category.gif"));
		setDescription(PAGE_DESC);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createControl(Composite parent) {
		Composite comp = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.numColumns = 3;
		comp.setLayout(layout);
		
		Label l;

		// name
		l = new Label(comp, SWT.NONE);
		l.setText("&Name");
		l.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,false));
		
		_name = new Text(comp, SWT.BORDER);
		_name.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false,2,1));
		_name.addModifyListener(_modify);

		// description
		l = new Label(comp, SWT.NONE);
		l.setText("&Description");
		l.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,false));
		
		_desc = new Text(comp, SWT.BORDER | SWT.MULTI);
		_desc.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,2,1));
		_desc.addModifyListener(_modify);
		
		// unit
		l = new Label(comp, SWT.NONE);
		l.setText("Unit");
		l.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,false));
		
		_unit = new Combo(comp, SWT.DROP_DOWN | SWT.READ_ONLY);
		initUnit();
		_unit.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,false));
		
		_name.setFocus();
		
		setControl(comp);
	}
	
	private IInventoryProvider getProvider(){
		if(_provider == null){
			try {
				_provider = InventoryManager.getInstance().getProvider();
			}catch(ProviderException e){
				setErrorMessage(e.getMessage());
			}
		}
		return _provider;
	}

	private void initUnit(){
		IInventoryProvider provider = getProvider();
		if(provider != null){
			try {
				List<Unit> units = provider.getUnits();
				for(Unit unit : units){
					_unit.add(unit.getAbbreviation());
					_unit.setData(unit.getAbbreviation(), unit);
				}
			}catch(ProviderException e){
				setErrorMessage(e.getMessage());
			}
		} else {
			setErrorMessage("Unable to obtain inventory provider.");
		}
	}
	
	private void updatePageComplete(){
		setPageComplete(isPageComplete());
	}
	
	public String getItemGroupName(){
		return _name.getText();
	}
	
	public String getItemGroupDescription(){
		return _desc.getText();
	}
}
